package org.jeecg.modules.oa.controller.appController;

import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 通讯录
 * @Author liurui
 * @Date 2020/1/3 14:50
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/b/addressBook")
public class AppAddressBookController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @RequestMapping({"list"})
    public Result addressList( HttpServletRequest request, HttpServletResponse response) {
        LoginUser user = SsoUtil.getLoginUser();
        if (user == null || StringUtils.isBlank(user.getId())) {
            return Result.error(99,"登录失效");
        }
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(user,sysUser);
        sysUser.setSqlMap(sysUserRoleService.dataScopeDepartOnlyFilter(user,"d","/isystem/sysUserList",null,null));
        if (StringUtils.isBlank(sysUser.getSqlMap())){
            sysUser.setSqlMap(" 1 = 1");
        } else {
            sysUser.setSqlMap(sysUser.getSqlMap().replace("AND", ""));
        }
        List<SysUser> list = sysUserService.findAddressBookList(sysUser);
        List<HashMap<String, String>> retList = new ArrayList<>(list.size());
        for (SysUser u : list) {
            try {
                HashMap<String, String> ele = setBookUser(u);
                retList.add(ele);
            } catch (Exception ex) {
                log.error("通讯录获取出错,name="+ u.getRealname() +"", ex);
            }
        }
        System.out.println(list.size() + "," + retList.size());
        return Result.ok(retList,"查询成功");
    }


    private HashMap<String, String> setBookUser(SysUser u) {
        HashMap<String, String> ele = new HashMap<>(7);
        ele.put("id", u.getId());								//ID
        ele.put("companyName", u.getCompanyName());		//归属公司
        ele.put("departName", u.getDepartName() == null ? "-" : u.getDepartName());			//归属部门
        ele.put("name", u.getRealname());							//姓名
//        ele.put("portraitPath", u.getPortraitPath() == null ? "" : (imageFileUrl + u.getPortraitPath()));	//用户头像
        ele.put("pinyin", StringUtils.getPinyin(u.getRealname()));	//拼音
        ele.put("phone", u.getPhone());						//手机
        ele.put("showMobile", "1");	//是否显示手机号   0:不可见,1:可见,2:申请中
        return ele;
    }

}
